package com.note.simpleadmin.module.shop.controller;

import com.note.simpleadmin.constant.ErrorCode;
import com.note.simpleadmin.entity.TbCoupon;
import com.note.simpleadmin.exception.DataRepeatException;
import com.note.simpleadmin.exception.ParamException;
import com.note.simpleadmin.module.shop.service.CouponService;
import com.note.simpleadmin.vo.Msg;
import com.note.simpleadmin.vo.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/***
 *@ClaseName:CouponController
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/2/28 0028 19:30
 */
@Controller
@RequestMapping(value = "shop/coupon")
public class CouponController {
    private final Logger logger = LoggerFactory.getLogger(ColumnController.class);
    @Autowired
    private CouponService couponService;
    @RequestMapping(value = "index")
    public String index() {
        return "shop/goods/coupon";
    }
    @RequestMapping(value = "addCoupon")
    public String addCoupon() {
        return "shop/goods/addCoupon";
    }
    @RequestMapping(value = "ajax_add")
    @ResponseBody
    public Msg ajax_add(TbCoupon coupon) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            couponService.save(coupon);
            msg.setMsg("已添加");
        } catch (DataRepeatException e) {
            logger.info(e.getMessage());
            msg.setMsg(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000);
        } catch (ParamException e) {
            logger.info(e.getMessage());
            msg.setMsg(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000);
        }catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "ajax_list")
    @ResponseBody
    public Msg ajax_list(String name, Integer page) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            PageBean pageBean =couponService.findByPage(name,page);
            msg.setData(pageBean);
            msg.setMsg("加载完成");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "detail")
    public String detail() {
        return "shop/goods/couponDetail";
    }
    @RequestMapping(value = "ajax_detail")
    @ResponseBody
    public Msg ajax_detail(String id) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            TbCoupon tbCoupon=couponService.getOne(id);
            msg.setData(tbCoupon);
            msg.setMsg("加载完成");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "forbidCoupon")
    @ResponseBody
    public Msg forbidCoupon(String id,Integer active) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            couponService.forbidCoupon(id,active);
            msg.setMsg("操作成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
}
